// src/stores/user.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  const profile = ref(JSON.parse(localStorage.getItem('user')) || null)

  const setUser = (userData) => {
    profile.value = userData
    localStorage.setItem('user', JSON.stringify(userData))
  }

  const logout = () => {
    profile.value = null
    localStorage.removeItem('user')
  }

  const currentUser = computed(() => profile.value)
  const isAuthenticated = computed(() => !!profile.value)

  return {
    profile,
    currentUser,
    isAuthenticated,
    setUser,
    logout
  }
})